package com.ruoyi.teach.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ruoyi.teach.mapper.TeachBookMapper;
import com.ruoyi.teach.mapper.TeachBookUserMapper;
import com.ruoyi.teach.domain.TeachBook;
import com.ruoyi.teach.domain.TeachBookUser;
import com.ruoyi.teach.service.ITeachBookService;

/**
 * 教材Service业务层处理
 * 
 * @author yepanpan
 * @date 2021-03-15
 */
@Service
public class TeachBookServiceImpl implements ITeachBookService 
{
    @Autowired
    private TeachBookUserMapper teachBookUserMapper;

    @Autowired
    private TeachBookMapper teachBookMapper;

    /**
     * 查询教材
     * 
     * @param id 教材ID
     * @return 教材
     */
    @Override
    public TeachBook selectTeachBookById(Long id)
    {
    	TeachBook book = teachBookMapper.selectTeachBookById(id);
    	
    	TeachBookUser uc = new TeachBookUser();
    	uc.setBookId(id);
    	List<TeachBookUser> list = teachBookUserMapper.selectTeachBookUserList(uc);
    	if(list != null && list.size() > 0) {
    		Long users[] = new Long[list.size()];
    		for(int i=0;i<list.size();i++) {
    			users[i] = list.get(i).getUserId();
    		}
    		book.setUsers(users);
    	}
    	
        return book;
    }

    /**
     * 查询教材列表
     * 
     * @param teachBook 教材
     * @return 教材
     */
    @Override
    public List<TeachBook> selectTeachBookList(TeachBook teachBook)
    {
        return teachBookMapper.selectTeachBookList(teachBook);
    }

    /**
     * 新增教材
     * 
     * @param teachBook 教材
     * @return 结果
     */
    @Override
    @Transactional
    public int insertTeachBook(TeachBook teachBook)
    {
    	int count = 0;
        teachBook.setCreateTime(DateUtils.getNowDate());
        teachBook.setCreateBy(SecurityUtils.getUsername());
        count += teachBookMapper.insertTeachBook(teachBook);
        for(Long uid:teachBook.getUsers()) {
        	TeachBookUser bu = new TeachBookUser();
        	bu.setBookId(teachBook.getId());
        	bu.setUserId(uid);
        	bu.setCreateBy(SecurityUtils.getUsername());
        	bu.setCreateTime(DateUtils.getNowDate());
        	teachBookUserMapper.insertTeachBookUser(bu);
        	count ++;
        }
        TeachBookUser bu = new TeachBookUser();
    	bu.setBookId(teachBook.getId());
    	bu.setUserId(SecurityUtils.getLoginUser().getUser().getUserId());
    	bu.setCreateBy(SecurityUtils.getUsername());
    	bu.setCreateTime(DateUtils.getNowDate());
    	teachBookUserMapper.insertTeachBookUser(bu);
    	count ++;
        return count;
    }

    /**
     * 修改教材
     * 
     * @param teachBook 教材
     * @return 结果
     */
    @Override
    @Transactional
    public int updateTeachBook(TeachBook teachBook)
    {
    	teachBookUserMapper.deleteTeachBookUsers(teachBook.getId());
        for(Long uid:teachBook.getUsers()) {
        	TeachBookUser bu = new TeachBookUser();
        	bu.setBookId(teachBook.getId());
        	bu.setUserId(uid);
        	bu.setCreateBy(SecurityUtils.getUsername());
        	bu.setCreateTime(DateUtils.getNowDate());
        	teachBookUserMapper.insertTeachBookUser(bu);
        }
        
        teachBook.setUpdateTime(DateUtils.getNowDate());
        teachBook.setUpdateBy(SecurityUtils.getUsername());
        return teachBookMapper.updateTeachBook(teachBook);
    }

    /**
     * 批量删除教材
     * 
     * @param ids 需要删除的教材ID
     * @return 结果
     */
    @Override
    public int deleteTeachBookByIds(Long[] ids)
    {
        return teachBookMapper.deleteTeachBookByIds(ids);
    }

    /**
     * 删除教材信息
     * 
     * @param id 教材ID
     * @return 结果
     */
    @Override
    public int deleteTeachBookById(Long id)
    {
        return teachBookMapper.deleteTeachBookById(id);
    }
}
